<?php
namespace app\api\controller\center;

use app\api\model\User as UserModel;
use think\facade\Request;
use think\response\Json;

class User
{
    /**
     * 获取用户个人信息
     * @return Json
     */
    public function profile(): Json
    {
        // 从请求中获取通过JWT中间件设置的user信息
        $userInfo = Request::user;
        
        // 查询用户详细信息
        $user = UserModel::field([
            'id', 'username', 'email', 'mobile', 
            'avatar', 'created_at', 'last_login_time', 'last_login_ip'
        ])->find($userInfo->user_id);
        
        if (!$user) {
            return json([
                'code' => 404,
                'message' => '用户不存在'
            ]);
        }
        
        return json([
            'code' => 200,
            'message' => '获取成功',
            'data' => $user
        ]);
    }
    
    /**
     * 新增用户信息
     * @return Json
     */
    public function add(): Json
    {
      
        $data = Request::only([
            'email', 'mobile', 'avatar', 
            'passwords', 'username'
        ], 'post');
        
        // 查找用户
        $username = UserModel::where('username',$data['username'])->find();
        if ($username) {
            return  output_error('用户名已被注册');
        }

        $mobile = UserModel::where('mobile',$data['mobile'])->find();
        if ($mobile) {
            return  output_error('手机号已经存在');
        }

        // 加密密码
        $data['passwords'] = password_hash($data['passwords'], PASSWORD_DEFAULT);

        // 过滤空值
        $data = array_filter($data, function($value) {
            return $value !== null && $value !== '';
        });

        // 更新用户信息
        if (!empty($data)) {
           (new UserModel)->save($data);
        }
        
        output_success('success', []);
    }

    /**
     * 更新用户信息
     * @return Json
     */
    public function update(): Json
    {
        $userInfo = Request::user;
        $data = Request::only([
            'email', 'mobile', 'avatar', 
            'password', 'new_password'
        ], 'post');
        
        // 查找用户
        $user = UserModel::find($userInfo->user_id);
        if (!$user) {
            return json([
                'code' => 404,
                'message' => '用户不存在'
            ]);
        }
        
        // 如果需要修改密码
        if (!empty($data['password']) && !empty($data['new_password'])) {
            // 验证旧密码
            if (!password_verify($data['password'], $user->password)) {
                return json([
                    'code' => 400,
                    'message' => '原密码不正确'
                ]);
            }
            
            // 更新密码
            $user->password = password_hash($data['new_password'], PASSWORD_DEFAULT);
            unset($data['password'], $data['new_password']);
        }
        
        // 过滤空值
        $data = array_filter($data, function($value) {
            return $value !== null && $value !== '';
        });
        
        // 更新用户信息
        if (!empty($data)) {
            $user->save($data);
        }
        
        return json([
            'code' => 200,
            'message' => '更新成功',
            'data' => [
                'id' => $user->id,
                'username' => $user->username,
                'email' => $user->email,
                'avatar' => $user->avatar,
                'mobile' => $user->mobile
            ]
        ]);
    }
}